{% extends "base.html" %}
{% import editor_macro(request) as editor %}

{% if request.query_params.get("parent_id") %}
    {% set action="答案" %}
{% else %}
    {% set action="校正" if write.wr_id else "写作" %}
{% endif %}

{% block head %}
    {{ editor.head() }}
    <link rel="stylesheet" href="{{ theme_asset(request, 'css/board_common.css?ver=1.0.0') }}">
    <link rel="stylesheet" href="{{ theme_asset(request, 'css/board_skin_basic.css?ver=1.0.0') }}">
{% endblock head %}

{% block title %}{{ board.subject }} 写作{{ action }}{% endblock title %}
{% block subtitle %}{{ board.subject }} 写作{{ action }}{% endblock subtitle %}

{% block content %}
{% set login_member=request.state.login_member %}
{% set is_admin=get_admin_type(request, login_member.mb_id, board=board) %}
{% set bo_table_width=board.bo_table_width if board.bo_table_width > 0 else 100 %}
<div id="bo_basic_write" class="bo_common bo_write">

    <form name="fwrite" id="fwrite" action="/board/write_update/{{ board.bo_table }}{% if write.wr_id %}/{{write.wr_id}}{% endif %}" method="post" enctype="multipart/form-data" onsubmit="return fwrite_submit(this);" autocomplete="off" style="width:{{ bo_table_width }}{% if bo_table_width <= 100 %}%{% else %}px{% endif %}">
        <input type="hidden" name="uid" value="{{ get_unique_id(request) }}">
        <input type="hidden" name="wr_id" value="{{ write.wr_id }}">
        <input type="hidden" name="parent_id" value="{{ request.query_params.parent_id }}">
        <input type="hidden" name="token" value="">
        {% if is_html and board.bo_use_dhtml_editor %}
        <input type="hidden" name="html" value="html1">
        {% endif %}
        {% if not is_admin and is_secret == 2 %}
        <input type="hidden" name="secret" value="secret">
        {% endif %}

        {% if categories %}
        <div class="write_select">

            <label for="ca_name" class="blind">分类<strong>必不可少的</strong></label>
            <select name="ca_name" id="ca_name" required class="required">
                <option value="">选择分类</option>
                {% for category in categories %}
                    <option value="{{ category }}" {{ get_selected(write.ca_name, category) }}>{{ category }}</option>
                {% endfor %}
            </select>

        </div>
        {% endif %}

        {% if not login_member or (write and write.mb_id != login_member.mb_id) %}
        <div class="write_writer">
            <label for="wr_name" class="blind">姓名<strong>必不可少的</strong></label>
            <input type="text" name="wr_name" value="{{ write.wr_name if write else login_member.mb_name }}" id="wr_name" required class="frm_input required" maxlength="20" placeholder="姓名">
            <label for="wr_password" class="blind">密码<strong>必不可少的</strong></label>
            <input type="password" name="wr_password" id="wr_password" {% if not request.state.is_super_admin %}required{% endif %} class="frm_input {% if not request.state.is_super_admin %}required{% endif %}" maxlength="20" placeholder="密码">
            <label for="wr_email" class="blind">电子邮件</label>
            <input type="email" name="wr_email" value="{{ write.wr_email if write else login_member.mb_email }}" id="wr_email" class="frm_input email" maxlength="100" placeholder="电子邮件">
            <label for="wr_homepage" class="blind">主页</label>
            <input type="text" name="wr_homepage" value="{{ write.wr_homepage if write else login_member.mb_homepage }}" id="wr_homepage" class="frm_input" placeholder="主页">
        </div>
        {% endif %}

        {% if is_notice or (is_html and not board.bo_use_dhtml_editor) or (is_admin and is_secret) or is_secret == 1 or is_mail %}
        <div class="write_option">
            <span class="blind">选项</span>

            <ul>
            {% if is_notice %}
                <li class="chk_box">
                    <input type="checkbox" id="notice" name="notice" value="1" {{ notice_checked }}>
                    <label for="notice"><span>&nbsp;</span>公共</label>
                </li>
            {% endif %}
            {% if is_html and not board.bo_use_dhtml_editor %}
                <li class="chk_box">
                    <input type="checkbox" id="html" name="html" onclick="html_auto_br(this);" value="{{ html_value }}" {{ html_checked }}>
                    <label for="html"><span>&nbsp;</span>html</label>
                </li>
            {% endif %}
            {% if (is_admin and is_secret) or is_secret == 1 %}
                <li class="chk_box">
                    <input type="checkbox" id="secret" name="secret" value="secret" {{ secret_checked }}>
                    <label for="secret"><span>&nbsp;</span>秘密写作</label>
                </li>
            {% endif %}
            {% if is_mail %}
                <li class="chk_box">
                    <input type="checkbox" id="mail" name="mail" value="mail" {{ recv_email_checked }}>
                    <label for="mail"><span>&nbsp;</span>收到您的回复邮件</label>
                </li>
            {% endif %}
            </ul>
        </div>
        {% endif %}

        <div class="write_subject">
            <label for="wr_subject" class="blind">标题<strong>必不可少的</strong></label>

            <input type="text" name="wr_subject" value="{{ write.wr_subject }}" id="wr_subject" required class="frm_input full_input required" size="50" maxlength="255" placeholder="标题">

            {% if login_member %}

            <script src="/static/js/autosave.js?ver=1.0.0"></script>
            <button type="button" id="btn_autosave_list" class="btn_frmline">临时存储写入 
                (<span id="autosave_count">0</span>)
            </button>
            <div id="autosave_pop">
                <strong>临时储存员额清单</strong>
                <ul>&nbsp;</ul>
                <div><button type="button" id="autosave_close">关闭</button></div>
            </div>

            {% endif %}
        </div>

        <!-- TODO: editor.body 面积 HTML 需要更正 -->
        <div>
            <label for="wr_content" class="blind">内容<strong>必不可少的</strong></label>
            <div class="wr_content">
                {% if write_min and write_max %}
                <p id="char_count_desc">这个公告板是最小的 <strong>{{ write_min }}</strong>超过字母, 最大值 <strong>{{ write_max }}</strong>你可以写字母或更少.</p>
                {% endif %}
                {% if write_min and write_max %}
                <div id="char_count_wrap"><span id="char_count"></span>字母</div>
                {% endif %}
            </div>
        </div>

        {{ editor.body("wr_content", write.wr_content) }}

        {% if is_link %}
            {% for i in range(1, 3) %}
            <div class="write_link">
                <i class="fa fa-link" aria-hidden="true"></i>
                <label for="wr_link{{ i }}" class="blind"> 链接 #{{ i }}</label>
                <input type="text" name="wr_link{{ i }}" value="{{ getattr(write, "wr_link" ~ i, "") }}" id="wr_link{{ i }}" class="frm_input full_input" size="50">
            </div>
            {% endfor %}
        {% endif %}

        {% if is_file %}
            {% for file in files %}
            {% set i = loop.index %}
            <div class="write_file">
                <i class="fa fa-folder-open" aria-hidden="true"></i>
                <label for="bf_file_{{ i }}" class="blind"> 文件 #{{ i }}</label>
                <input type="file" name="bf_file[]" id="bf_file_{{ i }}" title="文件的附件{{ i }} : 容量 {{ board.bo_upload_size }} 仅上传字节或更少" class="frm_file">
                {% if is_file_content %}
                <input type="text" name="bf_content[]" value="{{ file.bf_content|default("", true) }}" title="请输入文件说明." class="full_input frm_input" size="50" placeholder="请输入文件说明.">
                {% endif %}
                {% if file.bf_no != None %}
                <div class="file_del">
                    <input type="checkbox" id="bf_file_del{{ i }}" name="bf_file_del[]" value="{{ file.bf_no }}">
                    <label for="bf_file_del{{ i }}">
                        <span class="elip" title="{{ file.bf_source }}">{{ file.bf_source }}</span>
                        <span class="size">({{ file.bf_filesize|filesizeformat }})</span>
                        <span>删除文件</span>
                    </label>
                </div>
                {% endif %}
            </div>
            {% endfor %}
        {% endif %}

        <!-- Captcha Start -->
        {% if is_use_captcha %}
            {% include captcha_widget(request) ignore missing %}
        {% endif %}
        <!-- Captcha End -->

        <div class="btn_confirm">
            <a href="{{ url_for('list_post', bo_table=board.bo_table)|set_query_params(request, remove=['parent_id']) }}" class="btn_cancel btn">取消</a>
            <button type="submit" id="btn_submit" accesskey="s" class="btn_submit btn">完成</button>
        </div>

    </form>

    <script>
        {% if write_min and write_max %}
            // 有限的字符数
            var char_min = parseInt({{ write_min }}); // 最小值
            var char_max = parseInt({{ write_max }}); // 最大值
            var char_max = parseInt({{ write_max }}); // 최대
            check_byte("wr_content", "char_count");
        
            $(function() {
                $("#wr_content").on("keyup", function() {
                    check_byte("wr_content", "char_count");
                });
            });
        {% endif %}
        function html_auto_br(obj)
        {
            if (obj.checked) {
                result = confirm("你想做一个自动换行符吗?\n\n自动换行符是帖子内容之间换行的地方。<br>转换为标记的能力.");
                if (result) {
                    obj.value = "html2";
                } else{
                    obj.value = "html1";
                }
            } else {
                obj.value = "";
            }
        }

    function fwrite_submit(f)
    {
        if (document.getElementById("char_count")) {
            if (char_min > 0 || char_max > 0) {
                var cnt = parseInt(check_byte("wr_content", "char_count"));
                if (char_min > 0 && char_min > cnt) {
                    alert("内容是 "+char_min+"你需要写更多的信。.");
                    return false;
                }
                else if (char_max > 0 && char_max < cnt) {
                    alert("内容是 "+char_max+"你需要写的比字母少。.");
                    return false;
                }
            }
        }

        // captcha 使用时
        if (typeof check_captcha === "function") {
            if (!check_captcha(f)) {
                return false;
            }
        }

        f.token.value = generate_token();
        f.action += "?{{ request.query_params|safe }}"

        document.getElementById("btn_submit").disabled = true;

        return true;
    }
    </script>

</div>
{% endblock content %}